<template>
    <router-link class="user-bar" :to="{ name: currentLink }" tag="div">
   <div class="avatar">
       <template v-if="currentLink.name">
           <img src="/static/avatar.png" alt="avatar">
       </template>
       <template v-else>
           <img src="/static/user_normal.jpg" alt="未登录">
       </template>
   </div>
   <div class="holder">{{holder}}</div>
   <div class="icon icon-camera"></div>
   <div class="icon icon-pen"></div>
   </router-link>
</template>

<script>
import { mapGetters } from 'vuex';

   export default {
       name: 'user-bar',
       computed: {
         currentLink: function(){
           return this.currentUser.name ? 'HomeView' : 'LoginView'
         },
         holder: function(){
           return this.currentUser.name ? this.currentUser.name : '请先登录'
         },
         // 将 store 中的 getter 映射到局部计算属性：
         ...mapGetters(['currentUser'])
       },
       created(){
         // 获取到 email
         if(localStorage.getItem('email')){
           this.$store.commit({
             type: 'getLocalUser'
           })
         }
       }
 }
</script>

<style lang="scss" scoped>
.user-bar {
  position: relative;
  padding: 1rem 1.8rem;
  overflow: hidden;
  .avatar {
    width: 4rem;
    height: 4rem;
    float: left;
    img {
      width: 100%;
      height: 100%;
      border-radius: 100%;
    }
  }
  .holder {
    float: left;
    padding-left: 1rem;
    line-height: 4rem;
    font-size: 1.5rem;
    color: #aaa;
  }
  .icon {
    content: '';
    width: 4rem;
    height: 4rem;
    padding: 0.8rem;
    margin-left: 0.8rem;
    float: right;
    box-sizing: border-box;
  }
  .icon::before {
    content: '';
    position: absolute;
    width: 2.4rem;
    height: 2.4rem;
    background-position: center;
    background-repeat: no-repeat;
  }
  .icon-camera::before {
    background-image: url(/static/camera.svg);
  }
  .icon-pen::before {
    background-image: url(/static/pen.svg);
  }
  &:before {
    content: '';
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 0.1rem;
    background: #E8E8E8;
  }
}
</style>

